"""
Case Type   : 事件触发器
Case Name   : 【事件触发器】执行命令目标对象是共享对象，不会触发事件触发器
Create At   : 2024/10/15
Owner       : sungang14
Description :
    预置条件：
        1.创建事件触发器：
        CREATE OR REPLACE FUNCTION f_eventtrigger_0073()
        RETURNS event_trigger
        LANGUAGE plpgsql
        AS $$ BEGIN
        RAISE NOTICE 'test_event_trigger: % %', tg_event, tg_tag;
        END;
        $$;
        CREATE EVENT TRIGGER start_eventtri ON ddl_command_start EXECUTE PROCEDURE f_eventtrigger_0073();
        CREATE EVENT TRIGGER end_eventtri ON ddl_command_end EXECUTE PROCEDURE f_eventtrigger_0073();
        CREATE EVENT TRIGGER drop_eventtri ON sql_drop EXECUTE PROCEDURE f_eventtrigger_0073();
        CREATE EVENT TRIGGER rewrite_eventtri ON  table_rewrite EXECUTE PROCEDURE f_eventtrigger_0073();
    测试步骤：
        1.创建目标对象是共享对象，包含数据库、角色、表空间、事件触发器（CREAET DATABASE will change to CREATE SCHEMA when dolphin.b_compatibility_mode is on.）
        create database d_eventtrigger_0073;
        create role u_eventtrigger_0073 password 'Huawei@123';
        create tablespace ts_eventtrigger_0073 RELATIVE LOCATION 'tablespace/tablespace_0073';
        create event trigger et_eventtrigger_0073 on ddl_command_start execute procedure f_eventtrigger_0073();
        2、删除目标对象是共享对象，包含数据库、角色、表空间、事件触发器
        drop databse d_eventtrigger_0073;
        drop role u_eventtrigger_0073;
        drop tablespace ts_eventtrigger_0073;
        drop event trigger et_eventtrigger_0073;
Expect      : 
    1、不会触发事件触发器函数
    2、不会触发事件触发器函数
History     : 
"""

import sys
import os
import unittest
from yat.test import Node
from yat.test import macro

sys.path.append(sys.path[0] + "/../")
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH


class IndexFileDamaged(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f'-----{os.path.basename(__file__)} start-----')
        self.logger.info('----------------this is setup-----------------------')
        self.DB_ENV_PATH = macro.DB_ENV_PATH
        self.DB_INSTANCE_PATH = macro.DB_INSTANCE_PATH
        self.comsh = CommonSH('PrimaryDbBmode')
        self.fun_name = "f_eventtrigger_0073()"
        self.user_name = "u_eventtrigger_0073"
        self.db_name = "d_eventtrigger_0073"
        self.ts_name = "ts_eventtrigger_0073"
        self.et_name = "et_eventtrigger_0073"
        self.comsh.execut_db_sql("drop EVENT TRIGGER if exists start_eventtri;")
        self.comsh.execut_db_sql("drop EVENT TRIGGER if exists end_eventtri;")
        self.comsh.execut_db_sql("drop EVENT TRIGGER if exists drop_eventtri;")
        self.comsh.execut_db_sql("drop EVENT TRIGGER if exists rewrite_eventtri;")
        self.comsh.execut_db_sql(f'drop function if exists {self.fun_name};')
        self.comsh.execut_db_sql(f'drop role if exists {self.user_name};')
        self.comsh.execut_db_sql(f'drop database if exists {self.db_name};')
        self.comsh.execut_db_sql(f'drop tablespace if exists {self.ts_name};')
        self.comsh.execut_db_sql(f'drop event trigger if exists {self.et_name};')
        msg = self.comsh.execut_db_sql(
            f"CREATE OR REPLACE FUNCTION {self.fun_name} RETURNS event_trigger LANGUAGE plpgsql" \
            f" AS \$\$ BEGIN" \
            f" RAISE NOTICE 'test_event_trigger: % %', tg_event, tg_tag;" \
            f"END;" \
            f"\$\$;")
        self.logger.info(msg)
        self.assertIn("CREATE FUNCTION", msg)
        msg = self.comsh.execut_db_sql(
            f"CREATE EVENT TRIGGER start_eventtri ON ddl_command_start EXECUTE PROCEDURE {self.fun_name};")
        self.logger.info(msg)
        self.assertEqual("CREATE EVENT TRIGGER", msg)
        msg = self.comsh.execut_db_sql(
            f"CREATE EVENT TRIGGER end_eventtri ON ddl_command_end EXECUTE PROCEDURE {self.fun_name};")
        self.logger.info(msg)
        self.assertEqual("CREATE EVENT TRIGGER", msg)
        msg = self.comsh.execut_db_sql(
            f"CREATE EVENT TRIGGER drop_eventtri ON sql_drop EXECUTE PROCEDURE {self.fun_name};")
        self.logger.info(msg)
        self.assertEqual("CREATE EVENT TRIGGER", msg)
        msg = self.comsh.execut_db_sql(
            f"CREATE EVENT TRIGGER rewrite_eventtri ON table_rewrite EXECUTE PROCEDURE {self.fun_name};")
        self.logger.info(msg)
        self.assertEqual("CREATE EVENT TRIGGER", msg)

    def test_Index_file_damaged(self):
        self.logger.info('创建目标对象是共享对象，包含角色、表空间、事件触发器')
        msg = self.comsh.execut_db_sql(f"create ROLE {self.user_name} password 'Huawei@123';")
        self.logger.info(msg)
        self.assertIn(f'CREATE ROLE', msg)
        self.assertNotIn(f'test_event_trigger', msg)
        msg = self.comsh.execut_db_sql(f"create tablespace {self.ts_name} RELATIVE LOCATION 'tablespace/tablespace_0073';")
        self.logger.info(msg)
        self.assertIn(f'CREATE TABLESPACE', msg)
        self.assertNotIn(f'test_event_trigger', msg)
        msg = self.comsh.execut_db_sql(f'create event trigger {self.et_name} on ddl_command_start execute procedure {self.fun_name};')
        self.logger.info(msg)
        self.assertIn(f'CREATE EVENT TRIGGER', msg)
        self.assertNotIn(f'test_event_trigger', msg)

        self.logger.info('删除目标对象是共享对象，包含角色、表空间、事件触发器')
        msg = self.comsh.execut_db_sql(f'DROP ROLE {self.user_name};')
        self.logger.info(msg)
        self.assertIn(f'DROP ROLE', msg)
        self.assertNotIn(f'test_event_trigger', msg)
        msg = self.comsh.execut_db_sql(f'DROP tablespace {self.ts_name};')
        self.logger.info(msg)
        self.assertIn(f'DROP TABLESPACE', msg)
        self.assertNotIn(f'test_event_trigger', msg)
        msg = self.comsh.execut_db_sql(f'DROP event trigger {self.et_name};')
        self.logger.info(msg)
        self.assertIn(f'DROP EVENT TRIGGER', msg)
        self.assertNotIn(f'test_event_trigger', msg)

    def tearDown(self):
        self.logger.info('----------------this is tearDown-----------------------')
        # 删除事件触发器
        msg = self.comsh.execut_db_sql("drop EVENT TRIGGER if exists start_eventtri;")
        self.logger.info(msg)
        self.assertEqual('DROP EVENT TRIGGER', msg)
        msg = self.comsh.execut_db_sql("drop EVENT TRIGGER if exists end_eventtri;")
        self.logger.info(msg)
        self.assertEqual('DROP EVENT TRIGGER', msg)
        msg = self.comsh.execut_db_sql("drop EVENT TRIGGER if exists drop_eventtri;")
        self.logger.info(msg)
        self.assertEqual('DROP EVENT TRIGGER', msg)
        msg = self.comsh.execut_db_sql("drop EVENT TRIGGER if exists rewrite_eventtri;")
        self.logger.info(msg)
        self.assertEqual('DROP EVENT TRIGGER', msg)
        # 删除事件触发器函数
        msg = self.comsh.execut_db_sql(f"drop function if exists {self.fun_name};")
        self.logger.info(msg)
        self.assertEqual('DROP FUNCTION', msg)
        self.logger.info(f'-----{os.path.basename(__file__)} end-----')
